package com.atguigu.gmall.item.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author Aupt
 * @create 2022-06-05-9:39
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(new Supplier<Object>() {
            @Override
            public Object get() {

                System.out.println(Thread.currentThread().getName() + "CompletableFuture");
                //int i = 1 / 0;
                return 1024;
            }
        }).whenComplete(new BiConsumer<Object, Throwable>() {
            @Override
            public void accept(Object o, Throwable throwable) {
                System.out.println("----------o=" + o.toString());
                System.out.println("----------Throwable=" + throwable);
            }
        }).exceptionally(new Function<Throwable, Object>() {
            @Override
            public Object apply(Throwable throwable) {

                System.out.println("throwable=" + throwable);
                return 6666;
            }
        });

        System.out.println(completableFuture.get());

    }
}
